package com.example.footballsystem.service;

import com.example.footballsystem.entity.Field;
import com.example.footballsystem.entity.News;
import com.example.footballsystem.mapper.NewsMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Slf4j
@Service
public class NewsService {
    @Autowired
    NewsMapper newsMapper;
    public List<News> getAllNews(){
        return newsMapper.getAllNews();
    }
    //添加新闻
    public Boolean addNew(News news){
        Timestamp create_date = new Timestamp(System.currentTimeMillis());
        news.setCreateDate(create_date.toString());
        //log.info(news.toString());
        return newsMapper.addNew(news);
    }
    //删除
    public Boolean deleteNew(News news){
        return newsMapper.deleteNew(news.getNewID());
    }
    //搜索新闻
    public List<News> searchNews(String info){
        List<News> resultList = new ArrayList<>();
        int count = 0;

        if(!newsMapper.searchByTitle(info).isEmpty()){
            resultList.addAll((newsMapper.searchByTitle(info)));
            count++;

        }
        log.info("1"+newsMapper.searchByTitle(info).toString());
        try{
            if(!newsMapper.searchByNewID(Integer.parseInt(info)).isEmpty()){
                resultList.addAll((newsMapper.searchByNewID(Integer.parseInt(info))));
                count++;
            }
            log.info("2"+resultList.toString());
        }catch (NumberFormatException e) {
            if (!newsMapper.searchByContent(info).isEmpty()) {
                resultList.addAll((newsMapper.searchByContent("%"+info+"%")));
                count++;
            }
            log.info("3"+resultList.toString());
            for (int i = 0; i < count; i++) {
                for (int j = i + 1; j < count; j++) {
                    if (Objects.equals(resultList.get(j).getNewID(), resultList.get(j).getNewID())) {
                        //log.info(i + " " + j);
                        resultList.remove(j);
                        j--;
                        count--;
                    }
                }
            }
            //log.info(resultList.toString());
            return resultList;
        }
        if(!newsMapper.searchByContent(info).isEmpty()){
            resultList.addAll((newsMapper.searchByContent("%"+info+"%")));
            count++;

        }
        log.info("4"+resultList.toString());
        for (int i=0;i<count;i++) {
            for (int j=i+1;j<count;j++) {
                if(Objects.equals(resultList.get(j).getNewID(), resultList.get(j).getNewID())){
                    //log.info(i+" "+j);
                    resultList.remove(j);
                    j--;
                    count--;
                }
            }
        }
        log.info("5"+resultList.toString());
        //log.info(resultList.toString());
        return resultList;
    }
}
